package me.sluijsens.StartersQuiz;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;

/**
 * Listens for block events
 * @author Bryan Slop (Sluijsens)
 *
 */
public class BlockListener implements Listener {
	private StartersQuiz plugin;
	String tagChat;
	LangHandler lang;
	
	//Load configuration file
	private ConfigHandler conf;
	
	/**
	 * @param instance Instance of the plugin
	 */
	public BlockListener(StartersQuiz instance) {
		plugin = instance;
	}
	
	/**
	 * Triggered when a block is broken. Checks if the actions should be block, if so then cancel the event.
	 * @param event Event that occurred
	 */
	@SuppressWarnings("static-access")
	@EventHandler(priority = EventPriority.HIGH)
	public void onBlockBreak(BlockBreakEvent event) {
		Player player = event.getPlayer();
		tagChat = plugin.tagChat;
		lang = plugin.getLangHandler();
		conf = plugin.getConf();
		
		boolean config = conf.getBoolean("Block.Block-Breaking", true);
		
		if(plugin.checkBlock(player) && config) {
			event.setCancelled(true);
			player.sendMessage(tagChat + lang.getMessage("NotifyQuizFirst"));
		}
	}
	
	/**
	 * Triggered when a block is placed. Checks if the actions should be block, if so then cancel the event.
	 * @param event Event that occurred
	 */
	@SuppressWarnings("static-access")
	@EventHandler(priority = EventPriority.HIGH)
	public void onBlockPlace(BlockPlaceEvent event) {
		Player player = event.getPlayer();
		tagChat = plugin.tagChat;
		lang = plugin.getLangHandler();
		conf = plugin.getConf();
		
		boolean config = conf.getBoolean("Block.Block-Placing", true);
		
		if(plugin.checkBlock(player) && config) {
			event.setCancelled(true);
			player.sendMessage(tagChat + lang.getMessage("NotifyQuizFirst"));
		}
	}
}
